package edu.pku.sei.mte.mtemodel.model.collection;

import java.util.ArrayList;
import java.util.Collection;

public class OclCollection extends ArrayList<Object> {
	private static final long serialVersionUID = 8480495426159664084L;
	/*private DataType dataType = null; 
	public DataType getElementType(){
		return dataType;
	}
	public void setDataType(DataType dataType) {
		this.dataType = dataType;
	}*/
	private boolean isOrdered;
	private boolean isUnicity;
	
	public boolean isOrdered() {
		return isOrdered;
	}
	public boolean isUnicity() {
		return isUnicity;
	}
	static public OclCollection createOclBag(){
		OclCollection col = new OclCollection();
		col.isOrdered = false;
		col.isUnicity = false;
		//col.dataType = type;
		return col;
	}
	static public OclCollection createOclSet(){
		OclCollection col = new OclCollection();
		col.isOrdered = false;
		col.isUnicity = true;
		//col.dataType = type;
		return col;
	}
	static public OclCollection createOclOrderedSet(){
		OclCollection col = new OclCollection();
		col.isOrdered = true;
		col.isUnicity = true;
		//col.dataType = type;
		return col;
	}
	static public OclCollection createOclSequence(){
		OclCollection col = new OclCollection();
		col.isOrdered = true;
		col.isUnicity = false;
		//col.dataType = type;
		return col;
	}
	
	@Override
	public void add(int index, Object element) {
		if(isUnicity&&contains(element))
			return;
		super.add(index, element);
	}

	@Override
	public boolean add(Object e) {
		if(isUnicity&&contains(e))
			return false;
		return super.add(e);
	}

	@Override
	public boolean addAll(Collection<? extends Object> c) {
		if(isUnicity){
			for(Object o : c){
				add(o);
			}
			return true;
		} else return super.addAll(c);
	}

	@Override
	public boolean addAll(int index, Collection<? extends Object> c) {
		if(isUnicity){
			int i = index;
			int size = 0;
			for(Object o : c){
				size = this.size();
				add(i,o);
				if(size!=this.size())
					i++;
			}
			return i!=index;
		} else return super.addAll(index, c);
	}
	
	public int getOrder(Object o){
		return indexOf(o);
	}
	public boolean equals(Object o){
		if(o instanceof Collection)
			return super.equals(o);
		else {
			if(this.size()==1){
				return this.get(0).equals(o);
			} else
				return false;
		}
	}
}
